package org.lanqiao.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lanqiao.bean.EduManager;
import org.lanqiao.bean.EduStuCourseSel;
import org.lanqiao.bean.EduTeacherBean;
import org.lanqiao.bean.EduUserBean;
import org.lanqiao.bean.TeaCourse;
import org.lanqiao.dao.FactoryDao;
import org.lanqiao.myUtils.Myutils;
import org.lanqiao.service.FactoryService;

public class LoginServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// @Override
	// protected void doGet(HttpServletRequest req, HttpServletResponse resp)
	// throws ServletException, IOException {
	// // TODO Auto-generated method stub
	// super.doGet(req, resp);
	// }
	//
	// @Override
	// protected void doPost(HttpServletRequest req, HttpServletResponse resp)
	// throws ServletException, IOException {
	// // TODO Auto-generated method stub
	// super.doPost(req, resp);
	// }

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse Response)
			throws ServletException, IOException {
		// 获取两种不同的登录请求
		int log = Myutils.getInt(request.getParameter("log"));

		// 处理客户端发出的请求

		// 调用Model层完成请求

		// 解决编码方式的错误
		// request.setCharacterEncoding("utf-8");
		// 使用request获取客服端发出的数据
		String userName = request.getParameter("userName");

		request.getSession().setAttribute("username", userName);

		String userPsw = request.getParameter("userPassword");

		if (log == 1) {
			//管理员登录
			EduManager em = (EduManager) Myutils.getNewInstance(EduManager.class);

			em.setManagername(userName);

			em.setManagerpaw(userPsw);

			EduManager result = FactoryService.getUserInfoManageService().getManagerInfo(em);

			String strResult = "login.jsp";

			if (result != null) {
				// 登录成功以后跳转到这个页面
				strResult = "logined/logined.jsp";
				// 使用session将控制层的数据 传递到前台页面 使用el表达式进行解析				
				request.getSession().setAttribute("loginedManager", result);
				//获取所有的课程信息
				List tcall=FactoryService.getUserInfoManageService().getAllCou();
				//放入缓存中
				request.getSession().setAttribute("TeacherAllCourse", tcall);

			}
			// 使用forWord进行跳转
			request.getRequestDispatcher(strResult).forward(request, Response);
		} else if (log == 2) {
			// 学生登录系统
			// 实例化学生的对象
			EduUserBean eub = (EduUserBean) Myutils.getNewInstance(EduUserBean.class);
			// 把登录的用户名和密码存入变量中
			eub.setUsername(userName);
			eub.setUserpassword(userPsw);
			// 去验证数据库中是否有这个学生
			EduUserBean student = FactoryService.getLoginService().getStudentLogin(eub);
			if (student != null) {
				// 获取所有课程
				List ess = FactoryService.getUserInfoManageService().getStuCou(userName);
				// 学生的课程
				List stucourse = FactoryService.getUserInfoManageService().getStuCoIn(userName);

				request.getSession().setAttribute("stucourse", stucourse);

				request.getSession().setAttribute("StuCourSel", ess);

				request.getSession().setAttribute("StudentInfo", student);
				// 跳转到学生登录 的界面
				request.getRequestDispatcher("logined/stuLogined.jsp").forward(request, Response);
			} else {
				request.getRequestDispatcher("login.jsp").forward(request, Response);

			}
		} else if (log == 3) {
			// 将获取的数据填充到实体对象属性中
			EduTeacherBean etd = (EduTeacherBean) Myutils.getNewInstance(EduTeacherBean.class);
			etd.setTeachername(userName);
			etd.setTeacherpassword(userPsw);

			// 登录验证
			EduTeacherBean isLo = FactoryService.getLoginService().getTeacherLogined(etd);

			if (isLo != null) {
				//查询老师的课程
				List allTC=FactoryService.getUserInfoManageService().getATC(isLo.getTeacherid());
				
				request.getSession().setAttribute("allTC", allTC);
				
				request.getSession().setAttribute("TeacherBean", isLo);
				// 获取所有课程
				
				List tc=FactoryService.getUserInfoManageService().getTeaCourse(userName);
				//学生选择老师课程
				//List tgc=FactoryService.getUserInfoManageService().getStuCInfo(userName);
				
				//int thcAll=tgc.size();
				
				//request.getSession().setAttribute("stuAllScore", thcAll);
				
				//request.getSession().setAttribute("GiveGrade", tgc);
				
				
				request.getSession().setAttribute("teacourse", tc);
				
				request.getRequestDispatcher("logined/teaLogined.jsp").forward(request, Response);
			}
		}
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		super.destroy();
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		super.init(config);
	}

}
